import { http } from "@/utils/http";
import { vapUrlApi } from "../utils";

export type HostT = {
  hostId: string;
  md5: string;
  version: string;
  hostName: string;
  kernelVersion: string;
  kernelArch: string;
  ip: string;
  rdpPort: number;
  mac: string;
  os: string;
  CPUInfo: string;
  CPUCore: number;
  CPUHz: string;
  status: number;
  clusterID: string;
  bindUserID: string;
  createdAt: string;
  cpuPercent: number;
  memoryPercent: number;
};

export type AppIssueT = {
  appId: string;
  appName: string;
  alias: string;
  icon: string;
  linkPath: string;
  execPath: string;
  appParams: string;
  workSpace: string;
  displayMode: number;
  isAdmin: boolean;
  runIsolationLv: number;
  clusterId: string;
  isPublic: boolean;
};

export type ClusterT = {
  clusterId: string;
  name: string;
  cpuThreshold: number;
  memoryThreshold: number;
  timeOutAction: number;
  maxSessionTime: number;
  maxTimeOutTime: number;
  clusterType: number;
  creator: string;
  creatorId: string;
  createdAt: string;
  updatedAt: string;
  hosts: HostT[];
  apps: AppIssueT[];
};
export type AppInstallT = {
  id: string;
  hostId: string;
  appName: string;
  icon: string;
  linkPath: string;
  exePath: string;
  workSpace: string;
  appParams: string;
  pathStatus: boolean;
  isInstalled: boolean;
};

export type addClusterDto = {
  name: string;
  cpuThreshold: number;
  memoryThreshold: number;
  timeOutAction: number;
  maxSessionTime: number;
  maxTimeOutTime: number;
  clusterType: number;
};

// addCluster 添加集群
export const addCluster = (data?: addClusterDto) => {
  return http.request<string>("post", vapUrlApi("cluster/add/cluster"), {
    data
  });
};

// getClusterList 获取集群列表
export const getClusterList = () => {
  return http.request<any[]>("get", vapUrlApi("cluster/list/cluster"));
};
// 删除集群
export const deleteCluster = (id: string) => {
  return http.request("delete", vapUrlApi("cluster/" + id));
};

export type addHostDto = {
  hostName: string;
  ip: string;
  mac: string;
  domain: string;
  systemInfo: string;
  osType: number;
  buildNumber: string;
  version: string;
  guaIp: string;
  guaPort: number;
  guaProtocol: string;
  origin: number;
  status: number;
  username: string;
  password: string;
};

// addHost 添加主机
export const addHost = (data?: addHostDto) => {
  return http.request("post", vapUrlApi("cluster/add/host"), { data });
};

// getHostList 获取主机列表
export const getHostList = (name?: string) => {
  return http.request<any[]>("get", vapUrlApi("cluster/list/host"), {
    data: { name: name }
  });
};
// deleteHost 删除host
export const deleteHost = (id: string) => {
  return http.request("delete", vapUrlApi("cluster/host/" + id));
};

export type updateHostAppDto = {
  hostId: string;
  iconPath: string;
};
// updateHostAppList 通知主机更新应用列表
export const updateHostAppList = (data?: updateHostAppDto) => {
  return http.request("post", vapUrlApi("cluster/host/apps  "), { data });
};

export type trimHostDto = {
  hostId: string;
  Ip: string;
  hostname: string;
  guaIp: string;
  guaPort: number;
  guaProtocol: string;
};
// trimHostArgus 微调主机参数
export const trimHostArgus = (data?: trimHostDto) => {
  console.log(data);
  data.guaPort = Number(data.guaPort);
  return http.request("post", vapUrlApi("cluster/host/trim"), { data });
};

export type clusterBindHostsDto = {
  clusterId: string;
  hostIds: string[];
};

// bindHost 集群绑定主机
export const bindHost = (data?: clusterBindHostsDto) => {
  return http.request("post", vapUrlApi("cluster/bind/host"), { data });
};
// 获取集群主机列表：包含绑定的主机和未绑定的主机
export const getClusterHostList = (id: string) => {
  return http.request<any[]>("get", vapUrlApi("cluster/hosts/" + id));
};

export type issueAppDto = {
  appName: string;
  alias: string;
  icon: string;
  linkPath: string;
  execPath: string;
  appParams: string;
  workSpace: string;
  isAdmin: boolean;
  isSingle: boolean;
  isPublic: boolean;
  clusterId: string;
};

// issueApp 发布一个应用
export const issueApp = (data?: issueAppDto) => {
  return http.request("post", vapUrlApi("cluster/issue/app"), { data });
};
// getAppList 获取应用列表
export const getAppList = (name?: string) => {
  return http.request<any[]>("get", vapUrlApi("cluster/list/app"), {
    data: { name: name }
  });
};
// 删除应用
export const deleteApp = (id: string) => {
  return http.request("delete", vapUrlApi("cluster/app/" + id));
};
// 用户端获取应用列表
export const getMyApp = () => {
  return http.request<any[]>("get", vapUrlApi("cluster/my/app"));
};

// 检查会话复用
export const checkReuse = (id: string) => {
  return http.request<any>("post", vapUrlApi("session/check/" + id));
};

// 创建会话
export const createSession = (id: string) => {
  return http.request<any>("post", vapUrlApi("session/check/" + id));
};

export type adminSearchHostSession = {
  pageNum: number;
  pageSize: number;
  hostIp: string;
  status: string;
  startTime: string;
  endTime: string;
};
export type Result = {
  list: Array<any>;
  total?: number;
};
// getSessionPage 获取会话列表
export const getSessionPage = (params?: adminSearchHostSession) => {
  return http.request<Result>("get", vapUrlApi("session/page"), { params });
};

// getInstallAppList 获取集群下安装的应用列表
export const getInstallAppList = (clusterId?: string) => {
  return http.request<any[]>(
    "get",
    vapUrlApi("cluster/install/app/" + clusterId)
  );
};

// getAppIssueInfo 获取应用详细信息
export const getAppIssueInfo = (appId?: string) => {
  return http.request<any>("get", vapUrlApi("cluster/app/" + appId));
};

// bindWinSession 绑定win会话
export const bindWinSession = (
  hostSessionId?: string,
  winSessionId?: string
) => {
  return http.request("post", vapUrlApi("session/bind/winId"), {
    data: { hostSessionId, winSessionId }
  });
};

// insertAppOpenLog 插入应用打开日志
export const insertAppOpenLog = (appId?: string, hostSessionId?: string) => {
  return http.request("post", vapUrlApi("session/app/log"), {
    data: { appId, hostSessionId }
  });
};
// 注销会话
export const logoff = (id?: string) => {
  return http.request("post", vapUrlApi("session/logoff/" + id));
};
// 断开会话
export const breakSession = (id?: string) => {
  return http.request("post", vapUrlApi("session/break/" + id));
};

export type HintInstruct = {
  id: string;
  msg: string;
  title: string;
  msgType: string;
};

// 发送指令
export const sendInstruct = (hi: HintInstruct) => {
  return http.request("post", vapUrlApi("session/gua/msg"), { data: hi });
};
